/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.KeyEventPostProcessor;
import java.awt.KeyboardFocusManager;
import java.awt.TexturePaint;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;

import com.ruipengkj.ApplicationConfig;
import com.ruipengkj.ApplicationContext;
import com.ruipengkj.commons.util.DateFormatUtil;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.jframe.IContentPane;
import com.ruipengkj.component.jframe.IFrame;
import com.ruipengkj.component.jframe.IWindowTitleBar;
import com.ruipengkj.component.jframe.WindowChangeEvent;
import com.ruipengkj.component.jpanel.RepeatImagePanel;
import com.ruipengkj.component.tabbedPane.RPTabbedPane;
import com.ruipengkj.delegate.DialogDelegale;
import com.ruipengkj.factory.ImageFactory;
import com.ruipengkj.factory.UIFactory;
import com.ruipengkj.view.base.BasePagePanel;
import com.ruipengkj.view.base.BasePanel;

/**
 * 说明： 主窗体
 * 
 * @Author：李智深
 * @Date：2012-03-15
 */
public class RPMain extends IFrame implements DialogDelegale{
	private static final long serialVersionUID = 1L;

	private RPTabbedPane tabbedPane = new RPTabbedPane();//中间切换面板
	private JPanel contentPane = new JPanel(new BorderLayout());//主面板

	private RPSmaillToolBar smaillToolBar=new RPSmaillToolBar();
	
	private RPToolBar toolBar = new RPToolBar(smaillToolBar);//工具栏

	private JLabel timing_lib=new JLabel();//时间提示Label
	private Timer timing_timer=new Timer(1000, new TimingTimer());
	private JLabel date_lib = new JLabel("晚上好！");
	private JLabel user_lib=new JLabel(""+ApplicationContext.OPERATOR_NAME+"("+ApplicationContext.OPERATOR_ROLE_NAME+")");
	private JLabel quit_lib=new JLabel("退出");
	private JLabel help_lib=new JLabel("帮助");
	private JLabel lock_lib=new JLabel("锁屏");
	
	private int keyEsc = 0;
	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				RPMain rp = new RPMain();
				rp.setVisible(true);
			}
		});
	}

    /**
     * 权限测试
     */
//	static{
//		BuildSpringApplication.getInstance();
//		Iterator<Byte> it=ApplicationContext.RIGHT_TYPE_MAP().keySet().iterator();
//		while(it.hasNext()){
//			ApplicationContext.TRIGHT_LIST.add(it.next());
//		}
//	}
	
//	static{
//		BuildSpringApplication.getInstance();
//		Iterator<Byte> it=ApplicationContext.RIGHT_TYPE_MAP_MAIN().keySet().iterator();
//		while(it.hasNext()){
//			ApplicationContext.TRIGHT_LIST.add(it.next());
//		}
//	}
	
	public RPMain() {
		toolBar.setDialogDelegale(this);
		this.setUndecorated(true);
		this.setResizable(ApplicationConfig.APP_IS_RESIZABLE);
		this.setSize(ApplicationConfig.APP_WIDTH, ApplicationConfig.APP_HEIGHT);
		this.setLocation(0, 0);
		this.setTitle(ApplicationConfig.APP_TITLE);
		this.setIconImage(ApplicationConfig.APP_IMAGE);
		this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		this.setBackground(Util.CONTENT_PANE_BACKGROUND);
		this.init();
		initListener();
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				if (Util.showConfirmDialog("是否退出软件？") == 0) {
					System.exit(0);
				}
			}
		});
		
		KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager(); 
		manager.addKeyEventPostProcessor(new KeyEventPostProcessor(){

			@Override
			public boolean postProcessKeyEvent(KeyEvent e) {
				if(e.getKeyCode() == KeyEvent.VK_ESCAPE && keyEsc++ % 3 == 0){
					if (tabbedPane.getComponentCount() == 0 || tabbedPane.getSelectedIndex() == -1) {
						if(JOptionPane.showConfirmDialog(null, "是否要退出系统？","提示",JOptionPane.OK_CANCEL_OPTION)==0){
							System.exit(0);
						}
					} else {
						tabbedPane.removeTabAt(tabbedPane.getSelectedIndex());
					}
				}
				return false;
			}
			
		});
	}

	private void init() {
//		shortcutPane.setTitle("功能菜单");
		timing_timer.start();
		this.setContentPane(contentPane);
	
//		contentPane.add(this.topPanel(), BorderLayout.NORTH);// 头部兰
		this.setTitleBar(topPanel());
		// contentPane.add(new JPanel(), BorderLayout.SOUTH);// 状态栏

		IContentPane centerPane = new IContentPane() {

			private static final long serialVersionUID = 1L;
			@Override
			protected void paintBorder(Graphics g) {
//				super.paintBorder(g);
				g.setColor(new Color(97, 145, 193));
				int width = this.getWidth();
				int height = this.getHeight();
				g.drawLine(0, 0, 0, height);
				g.drawLine(0, height-1, width, height-1);
				g.drawLine(width-1, 0, width - 1, height-1);
			}
		};
		centerPane.setBorder(BorderFactory.createEmptyBorder(0, 1, 1, 1));
		centerPane.setLayout(new BorderLayout());
		centerPane.setOpaque(true);
//		centerPane.setBackground(Util.CONTENT_PANE_BACKGROUND);
//		centerPane.setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
		centerPane.add(smaillToolBar, BorderLayout.NORTH);
		// centerPane.add(outlookPane, BorderLayout.WEST);
		centerPane.add(tabbedPane, BorderLayout.CENTER);
		this.setIContentPane(centerPane);
		contentPane.add(centerPane, BorderLayout.CENTER);
//		tabbedPane.addTab("测试测试测试", new JLabel());
//		this.add(contentPane);
		toolBar.showData();
	}
	
	//头部面板
	private IWindowTitleBar topPanel(){
		//主面板
		IWindowTitleBar topPane = new IWindowTitleBar(){
			private static final long serialVersionUID = 1L;
			private JButton minButton = UIFactory.getInstance().createJButton("", 
					ImageFactory.loadImage(ImageFactory.BTN_TITLE_MIN), ImageFactory.loadImage(ImageFactory.BTN_TITLE_MIN_ENTER), 19, 34, this);
			private JButton maxButton = UIFactory.getInstance().createJButton("", 
					ImageFactory.loadImage(ImageFactory.BTN_TITLE_MAX), ImageFactory.loadImage(ImageFactory.BTN_TITLE_MAX_ENTER), 19, 35, this);
			private JButton closeButton = UIFactory.getInstance().createJButton("", 
					ImageFactory.loadImage(ImageFactory.BTN_TITLE_CLOSE), ImageFactory.loadImage(ImageFactory.BTN_TITLE_CLOSE_ENTER), 19, 46, this);
			private ImageIcon backgroundImageIcon = ImageFactory.loadImage(ImageFactory.TOOLBAR_BG);
			private TexturePaint paint = Util.createTexturePaint(backgroundImageIcon.getImage());
			private Image backgroundLeftImage = ImageFactory.loadImage(ImageFactory.TOOLBAR_LEFT_BG).getImage();
			private Image backgroundRightImage = ImageFactory.loadImage(ImageFactory.TOOLBAR_RIGHT_BG).getImage();
			@Override
			public void aa() {
				JPanel jp = new JPanel(null);
				Util.setSize(jp, 115, 19);
				jp.setOpaque(false);
				jp.add(minButton);
				jp.add(maxButton);
				jp.add(closeButton);
				minButton.setBounds(0, 0, 34, 19);
				maxButton.setBounds(34, 0, 35, 19);
				closeButton.setBounds(69, 0, 46, 19);
				maxButton.setName("0");
				add(jp, BorderLayout.EAST);
				this.setMaxButton(maxButton);
			}
			@Override
			protected void paintComponent(Graphics g) {
			        Graphics2D g2d = (Graphics2D) g;
			        //fill background.
			        g2d.setPaint(paint);
			        g2d.fillRect(0, 0, this.getWidth(), this.getHeight());
			        //draw right image.
			        int x = this.getWidth() - backgroundRightImage.getWidth(null);
			        int y = 0;
			        g2d.drawImage(backgroundRightImage, x, y, null);
			        //draw left image.
			        g2d.drawImage(backgroundLeftImage, 0, 0, null);
			        super.paintComponent(g);
			}
			@Override
			protected void paintBorder(Graphics g) {
				super.paintBorder(g);
				int width = this.getWidth();
				int height = this.getHeight();
				
				g.setColor(new Color(136, 207, 251));
				g.drawLine(0, 0, width-1, 0);
				
				g.setColor(new Color(97, 145, 193));
				g.drawLine(0, 0, 0, height-1);
				g.drawLine(width-1, 0, width - 1, height-1);
			}
			
			public void actionPerformed(ActionEvent e)
			{
				JButton b = (JButton)e.getSource();
				if (b == minButton)
				{
					fireWindowChangeEvent(WindowChangeEvent.WINDOW_MINIMIZED, new WindowChangeEvent());
				}
				else if (b == maxButton)
				{
					changeRestoreButton();
					fireWindowChangeEvent(WindowChangeEvent.WINDOW_MAXIMIZED, new WindowChangeEvent());	
				}
				else if (b == closeButton)
				{
					fireWindowChangeEvent(WindowChangeEvent.WINDOW_CLOSED, new WindowChangeEvent());
				}
			}
		};
		topPane.setLayout(new BorderLayout());
		
		topPane.setOpaque(false);
		topPane.setBackground(Util.CONTENT_PANE_BACKGROUND);
		topPane.setBorder(BorderFactory.createEmptyBorder(1, 1, 0, 1));
		topPane.add(toolBar,BorderLayout.CENTER);//工具栏
		topPane.aa();
		//状态栏面板
		RepeatImagePanel repeatImagePanel=new RepeatImagePanel(ImageFactory.loadImage(ImageFactory.TITLE_STATUS_BG).getImage());
		repeatImagePanel.setPreferredSize(new Dimension(0, 37));
		repeatImagePanel.setOpaque(true);
		repeatImagePanel.setBackground(Util.CONTENT_PANE_BACKGROUND);
		repeatImagePanel.setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
		repeatImagePanel.setLayout(new GridLayout(1, 2));
		
		timing_lib.setIcon(ImageFactory.loadImage(ImageFactory.ICON_1));
		timing_lib.setText(DateFormatUtil.formatK(new Date()));
		timing_lib.setIconTextGap(JLabel.LEFT);
		repeatImagePanel.add(timing_lib);
		
		JPanel jp=new JPanel(new FlowLayout(FlowLayout.RIGHT));
		jp.setOpaque(false);
		jp.add(date_lib);
		jp.add(user_lib);
		jp.add(quit_lib);
		jp.add(help_lib);
		jp.add(lock_lib);
		repeatImagePanel.add(jp);
		Util.setSize(topPane, 0, 135);
		topPane.add(repeatImagePanel,BorderLayout.SOUTH);
		return topPane;
	}
	
	//初始化监听器
	private void initListener() {
		mouseListener mouseListener = new mouseListener();
		quit_lib.addMouseListener(mouseListener);
		help_lib.addMouseListener(mouseListener);
		lock_lib.addMouseListener(mouseListener);
	}
	
	private class mouseListener implements MouseListener{
		@Override//鼠标点击
		public void mouseClicked(MouseEvent e) {
			if (e.getSource() == quit_lib) {
				if (Util.showConfirmDialog("是否退出软件？") == 0) {
					System.exit(0);
				}
			} else if (e.getSource() == help_lib) {
				JOptionPane.showMessageDialog(null, "本软件如有问题，请登陆：http://www.dlwoo.com\n售后电话：0755-27663600\nQQ：2396538297");
			} else if (e.getSource() == lock_lib) {
				//锁屏
				String name=JOptionPane.showInputDialog("请输入锁屏密码");
				if(name!=null) {
					ApplicationContext.lockPassword=name;
					while(true){
						String pasd=JOptionPane.showInputDialog("请输入解屏密码");
						if(pasd!=null&&(ApplicationContext.lockPassword.equals(pasd))){
							break;
						}
					}
				}
			}
		}

		@Override//鼠标进入
		public void mouseEntered(MouseEvent e) {
			if (e.getSource() == quit_lib) {
				quit_lib.setForeground(Color.red);
			}
			if (e.getSource() == help_lib) {
				help_lib.setForeground(Color.red);
			}
			if (e.getSource() == lock_lib) {
				lock_lib.setForeground(Color.red);
			}
		}

		@Override//鼠标退出
		public void mouseExited(MouseEvent e) {
			if (e.getSource() == quit_lib) {
				quit_lib.setForeground(Color.black);
			}
			if (e.getSource() == help_lib) {
				help_lib.setForeground(Color.black);
			}
			if (e.getSource() == lock_lib) {
				lock_lib.setForeground(Color.black);
			}
		}

		@Override//鼠标压下
		public void mousePressed(MouseEvent e) {
		}

		@Override//鼠标放开
		public void mouseReleased(MouseEvent e) {
		}
	}
	
	class TimingTimer implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			timing_lib.setText(DateFormatUtil.formatK(new Date()));
			date_lib.setText(ApplicationContext.TIME_NAME(new SimpleDateFormat("HH").format(new Date())));
		}
	}
	private BasePagePanel<?> basePagePanel=null;
	private BasePanel basePanel=null;
	@Override
	public void addJComponent(JComponent comp) {
		if(comp !=null){
			if(comp instanceof BasePagePanel){
				basePagePanel=(BasePagePanel<?>)comp;
				basePagePanel.clear();
			}else if(comp instanceof BasePanel){
				basePanel=(BasePanel)comp;
				basePanel.clear();
			}
			tabbedPane.addTab(comp.getName(), comp);
			tabbedPane.setSelectedComponent(comp);
		}
	}

	@Override
	public JComponent getParentJComponent() {
		// TODO Auto-generated method stub
		return null;
	}
}
